package cn.jsony.system.service.impl;

import cn.jsony.system.domain.Role;
import cn.jsony.system.mapper.RoleMapper;
import cn.jsony.system.service.IRoleService;
import cn.jsony.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void add(Role role) {
        super.add(role);
        addRoleMenuAndPermission(role);
    }

    @Override
    public void del(Long id) {

        roleMapper.delRoleMenuByRoleId(id);
        roleMapper.delRolePermissionByRoleId(id);
        //删除员工角色中间表
        roleMapper.delEmployeeRoleByRoleId(id);
        super.del(id);
    }

    @Override
    public void update(Role role) {
        super.update(role);
        //先删除旧的权限和菜单
        //删除角色菜单中间表
        roleMapper.delRoleMenuByRoleId(role.getId());
        //删除角色权限中间表
        roleMapper.delRolePermissionByRoleId(role.getId());

        addRoleMenuAndPermission(role);
    }
    public void addRoleMenuAndPermission(Role role){
        List<Long> menus = role.getMenus();
        if(menus.size()>0){//当前角色选择了菜单
            roleMapper.addRoleMenu(role);
        }
        List<Long> permissions = role.getPermissions();
        if(permissions.size()>0){//当前选择寻找了菜单
            roleMapper.addRolePermission(role);
        }
    }
}
